package com.example.controller;

import com.example.common.Result;
import com.example.entity.Schools;
import com.example.service.SchoolsService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 帖子前端接口
 */
@RestController
@RequestMapping("/schools")
public class SchoolsController {

    @Resource
    private SchoolsService schoolsService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Schools schools) {
        schoolsService.add(schools);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        schoolsService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        schoolsService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Schools schools) {
        schoolsService.updateById(schools);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Schools schools = schoolsService.selectById(id);
        return Result.success(schools);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Schools schools) {
        List<Schools> schoolsList = schoolsService.selectAll(schools);
        return Result.success(schoolsList);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Schools schools,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Schools> pageInfo = schoolsService.selectPage(schools, pageNum, pageSize);
        return Result.success(pageInfo);
    }
}
